import {
  Controller,
  Get,
  Post,
  Body,
  Patch,
  Param,
  Delete,
  Req,
} from '@nestjs/common';
import {
  ApiOperation,
  ApiResponse,
  ApiBody,
  ApiParam,
  ApiTags,
} from '@nestjs/swagger';
import { UserService } from './user.service';
import { CreateUserDto } from './dto/create-user.dto';
import { UpdateUserDto } from './dto/update-user.dto';
import { User } from './entities/user.entity';
import { HasRoles } from '../role/role.decorator';
import { AssignRoleDto } from './dto/assign-role.dto';
import { Public } from '../auth/auth.decorator';
import { HasPermissions } from '../permission/permission.decorator';

@Controller('user')
@ApiTags('用户管理') // 为控制器添加标签
// @HasRoles(['ADMIN'])
export class UserController {
  constructor(private readonly userService: UserService) {}

  @Post('assign-role')
  @ApiOperation({ summary: '为用户分配角色' })
  @ApiBody({ type: AssignRoleDto, description: '为用户分配角色的详细信息' })
  @ApiResponse({ status: 200, description: '角色分配成功', type: User })
  @ApiResponse({ status: 404, description: '用户或角色未找到' })
  async assignRole(@Body() assignRoleDto: AssignRoleDto) {
    return this.userService.assignRole(assignRoleDto);
  }

  // 创建单个用户
  @Post()
  @Public()
  @ApiOperation({ summary: '创建用户' })
  @ApiBody({ type: CreateUserDto, description: '创建用户的详细信息' })
  @ApiResponse({ status: 201, description: '用户创建成功', type: User })
  create(@Body() createUserDto: CreateUserDto) {
    return this.userService.create(createUserDto);
  }

  // 查询所有用户
  @Get()
  @HasPermissions('system:user:view')
  @ApiOperation({ summary: '获取所有用户' })
  @ApiResponse({ status: 200, description: '返回所有用户数据', type: [User] })
  findAll() {
    return this.userService.findAll();
  }

  @Post('info')
  @ApiOperation({ summary: '获取所有用户info' })
  @ApiResponse({ status: 200, description: '返回所有用户数据', type: [User] })
  info(@Req() req: Request) {
    return req['user'] || null;
  }

  // 根据ID查询单个用户
  @Get(':id')
  @Public()
  @ApiOperation({ summary: '根据ID获取用户' })
  @ApiParam({ name: 'id', type: String, description: '用户ID' })
  @ApiResponse({ status: 200, description: '返回用户数据', type: User })
  findOne(@Param('id') id: string) {
    return this.userService.findOne(id);
  }

  // 更新用户
  @Patch(':id')
  @ApiOperation({ summary: '更新用户' })
  @ApiParam({ name: 'id', type: String, description: '用户ID' })
  @ApiBody({ type: UpdateUserDto, description: '更新用户的详细信息' })
  @ApiResponse({ status: 200, description: '用户更新成功', type: User })
  update(@Param('id') id: string, @Body() updateUserDto: UpdateUserDto) {
    return this.userService.update(id, updateUserDto);
  }

  // 删除用户
  @Delete(':id')
  @ApiOperation({ summary: '删除用户' })
  @ApiParam({ name: 'id', type: String, description: '用户ID' })
  @ApiResponse({ status: 200, description: '用户删除成功' })
  remove(@Param('id') id: string) {
    return this.userService.remove(id);
  }

  // 批量添加用户
  @Post('bulk-create')
  @ApiOperation({ summary: '批量添加用户' })
  @ApiBody({
    type: [CreateUserDto],
    description: '批量创建用户的详细信息',
  })
  @ApiResponse({
    status: 201,
    description: '用户批量创建成功',
    type: [User],
  })
  async bulkCreate(@Body() createUserDtos: CreateUserDto[]) {
    return await this.userService.createBulk(createUserDtos);
  }

  // 批量删除用户
  @Delete('bulk-delete')
  @ApiOperation({ summary: '批量删除用户' })
  @ApiBody({
    type: [String],
    description: '批量删除用户ID列表',
  })
  @ApiResponse({
    status: 200,
    description: '用户批量删除成功',
  })
  async bulkDelete(@Body() ids: string[]) {
    return await this.userService.removeBulk(ids);
  }
}
